#!/usr/bin/env python
# coding: utf-8

# # 高德api练习

# ## 地理编码

# In[1]:


import requests,json
import pandas as pd
gao_key = "030d60847cc2dbf652a0d1f7e0fce690"


# In[10]:


中大南方_address = "广东省广州市中山大学南方学院"
暨南大学_address = "广东省广州市暨南大学"
猎德_address = "广东省广州市猎德"


# In[18]:


地理编码_url = "https://restapi.amap.com/v3/geocode/geo?parameters"
params = {
    "key":gao_key,
    "address":中大南方_address,
    
}
# positional argument follows keyword argument
# pa 一定要放在前面，keyword argument放在末尾！！！可以
response = requests.get(地理编码_url,params=params)
response


# In[19]:


response.json()


# In[13]:


暨南大学_location = response.json()['geocodes'][0]['location']
暨南大学_location


# In[15]:


猎德_location = response.json()['geocodes'][0]['location']
猎德_location


# In[20]:


中大南方_location = response.json()['geocodes'][0]['location']
中大南方_location


# ## 逆地理编码

# In[21]:


def regeo(key,location,extensions="base"):
    url:"https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":gao_key,
        "address":address,
        "extensions":extensions,
    }
    r = requests.get(url,params)
    results = r.json()
    return results


# In[22]:


url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
params = {
    "key":gao_key,
    "location":中大南方_location,
    "extensions":"all"
}
r = requests.get(url,params)
r


# In[23]:


_roads_ = []
for item in r.json()['regeocode']['roads']:
#     print(item['name'])
    _roads_.append(item['name'])
_roads_


# In[24]:


r.json()


# In[25]:


a=[]
for i in r.json()['regeocode']['pois'][6]['name']:
    a.append(i)
print(a)
        


# In[26]:


name=[]
name.append(r.json()['regeocode']['pois'][0]['name'])
name.append(r.json()['regeocode']['pois'][1]['name'])
name.append(r.json()['regeocode']['pois'][2]['name'])
name.append(r.json()['regeocode']['pois'][3]['name'])
name.append(r.json()['regeocode']['pois'][4]['name'])
name.append(r.json()['regeocode']['pois'][5]['name'])


# In[27]:


print(name)


# ## 路径规划

# In[28]:


origin = "113.679287,23.632575"
destination = "113.668051,23.600869"


# In[29]:


def geocode(key,address,city=None,batch=None)->str:
    """获取高德API的地理编码
    注释：key是指高德API的秘钥，请先去注册
          address是指结构化地址，具体请查看：https://lbs.amap.com/api/webservice/guide/api/georegeo   
    """
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key":gao_key,
        "address":address,
        "city":city,
        "batch":batch
    }
    response = requests.get(url,params=params)
    results = response.json()['geocodes'][0]['location']
    # 返回值
    return results


# In[30]:


response.json()


# In[31]:


起始点_中大南方 = origin
起始点_中大南方


# In[32]:


终点_龙岗镇 = destination
终点_龙岗镇


# ### 步行

# In[36]:


bx_url = "https://restapi.amap.com/v3/direction/walking?parameters"
def buxing(gao_key,origin,destination):
    params = {
    "key":gao_key,
    "origin":起始点_中大南方,
    "destination":终点_龙岗镇,
}
    result = requests.get(bx_url,params)
    result = r.json()
    return result


# In[32]:


buxing = buxing(gao_key,origin,destination)
buxing.json()


# ### 公交

# In[81]:


origin = "113.350961,23.131577"
destination = "113.336127,23.113409"
city =  "广州"


# In[82]:


暨南大学_address = "广东省广州市暨南大学"
猎德大桥_address = "广东省广州市猎德大桥"
def bus(gao_key,origin,destination,city):
    gj_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key":gao_key,
        "origin":origin,
        "destination":destination,
        "city":city,
    }
    r = requests.get(gj_url,params=params)
    result = r.json()
    return result


# In[85]:


a=bus(gao_key,origin,destination,city)
a


# In[93]:


pd.json_normalize(a["route"]["transits"])


# In[94]:


方案一 = pd.json_normalize(a["route"]["transits"][0])
方案一


# In[95]:


pd.json_normalize(a["route"]["transits"])["cost"]


# ## 行政区域查询

# In[43]:


xz_url="https://restapi.amap.com/v3/config/district?parameters"
keywords="广东"
subdistrict="3"


# In[44]:


def xingzheng(gao_key,keywords,subdistrict):
    params = {
        "key":gao_key,
        "keywords":keywords,
        "output":"json",
        "subdistrict":subdistrict,
    }
    result = requests.get(xz_url,params=params)
    return result


# In[45]:


b = xingzheng(gao_key,keywords,subdistrict)
b


# In[46]:


b.json()


# ## 搜索poi

# ### 关键字

# In[68]:


poi_url="https://restapi.amap.com/v3/config/district?parameters"
keywords = "大学"
city = "广州"
citylimit = "true"


# In[69]:


def poi(keywords,city,citylimit=None):
    params={
    "key":gao_key,
    "keywords":keywords,
    "city":city,
    "citylimit":citylimit,
}
    result = requests.get(poi_url,params=params)
    return result


# In[70]:


poi = poi(keywords,city,citylimit=None)
poi


# In[71]:


poi.json()


# ### 周边搜索

# In[3]:


zb_url = "https://restapi.amap.com/v3/place/around?parameters "
location = "113.679287,23.632575"
city = "广州"


# In[4]:


def zhoubian(gao_key,location,city):
    params = {
        "key":gao_key,
        "location":location,
        "city":city,
    }
    r = requests.get(zb_url,params=params)
    return r


# In[6]:


zb = zhoubian(gao_key,location,city)
zb.json()


# ### 多边形

# In[7]:


db_url = "https://restapi.amap.com/v3/place/polygon?parameters "
polygon = "116.460988,40.006919|116.48231,40.007381;116.47516,39.99713|116.472596,39.985227|116.45669,39.984989|116.460988,40.006919"
keywords = "711"


# In[8]:


def duobianxing(gao_key,polygon,keywords):
    params = {
        "key":gao_key,
        "polygon":polygon,
        "keywords":keywords,
    }
    t = requests.get(db_url,params=params)
    return t


# In[9]:


db = duobianxing(gao_key,polygon,keywords)
db.json()


# ### ID查询

# In[15]:


id_url = "https://restapi.amap.com/v3/place/detail?parameters "
id = "B0FFFAB6J2"


# In[16]:


def idchaxun(gao_key,id):
    params = {
        "key":gao_key,
        "id":id,
    }
    e = requests.get(id_url,params=params)
    return e


# In[17]:


idchaxun = idchaxun(gao_key,id)
idchaxun.json()


# ## ip定位

# In[18]:


ip_url = "https://restapi.amap.com/v3/ip?parameters"
ip = "119.29.115.93"


# In[19]:


def ipdingwei(gao_key,ip):
    params = {
        "key":gao_key,
        "ip":ip,
    }
    p = requests.get(ip_url,params=params)
    return p


# In[20]:


ipdingwei = ipdingwei(gao_key,ip)
ipdingwei.json()


# ## 静态地图

# In[110]:


jt_url = "https://restapi.amap.com/v3/staticmap?parameters"
location = "113.679287,23.632575"
zoom = "1,17"
scale = "2"


# In[111]:


params={
    "key":gao_key,
    "location":location,
    "zoom":zoom,
    "scale":scale,
}
r = requests.get(jt_url,params=params)


# In[112]:


r


# In[113]:


r.json()


# In[104]:


def jingtai(gao_key,location,zoom,scale):
    params ={
        "key":gao_key,
        "location":location,
        "zoom":zoom,
        "scale":scale,
    }
    r = requests.get(jt_url,params=params)
    result = r.json()
    return result


# In[105]:


a = jingtai(gao_key,location,zoom,scale)
a


# ## 坐标转换

# In[119]:


zb_url="https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
locations="116.481499,39.990475|116.481499,39.990375"
coordsys="gps"


# In[122]:


def zuobiao(gao_key,locations,coordsys=None):
    params={
        "key":gao_key,
        "locations":locations,
        "coordsys":coordsys,
    }
    r=requests.get(zb_url,params=params)
    return r


# In[123]:


e = zuobiao(gao_key,locations,coordsys=None)
e


# In[124]:


e.json()


# ## 天气

# In[31]:


tq_url="https://restapi.amap.com/v3/weather/weatherInfo?parameters"
city = "440100"
extensions="all"


# In[32]:


def tianqi(gao_key,city,extensions):
    params={
    "key":gao_key,
    "city":city,
    "extensions":extensions,}
    r = requests.get(tq_url,params=params)
    return r


# In[33]:


a = tianqi(gao_key,city,extensions)
a


# In[34]:


a.json()


# ## 输入提示

# In[218]:


keywords="麦当劳"
location="113.2759952545166,23.117055306224895"
city="广州"
citylimit="true"
output="json"
gao_key="030d60847cc2dbf652a0d1f7e0fce690"


# In[219]:


def tishi(gao_key,keywords,location,city):
    tishi_url="https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params={
        "keywords":keywords,
        "key":gao_key,
        "location":location,
        "city":city,
        "output":output,
    }
    t=requests.get(tishi_url,params=params)
    return t


# In[220]:


a = tishi(gao_key,keywords,location,city)
a.json()


# ## 交通态势

# In[232]:


jiaotong_url="https://restapi.amap.com/v3/traffic/status/rectangle?parameters"
extensions="all"


# In[233]:


def jiaotong(gao_key,level,extensions):
    params={
        "key":gao_key,
        "extensions":extensions,
    }
    r = requests.get(jiaotong_url,params=params)
    return r


# In[234]:


jiaotong = jiaotong(gao_key,level,extensions)
jiaotong.json()


# In[ ]:





# In[ ]:




